package com.tools.common.result;

import com.tools.common.enums.ResultCode;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.time.LocalDateTime;

@Getter
@Setter
public class Result<T>  implements Serializable {


    /**
     * 状态码
     */
    private int code;

    /**
     * 提示信息
     */
    private String message;

    /**
     * 响应数据
     */
    private T data;

    /**
     * 响应时间戳
     */
    private LocalDateTime timestamp;


    private boolean success;

    private boolean shouldCompensate;


    // 私有构造方法，防止直接实例化
    private Result() {
        this.timestamp = LocalDateTime.now();
    }

    /**
     * 成功响应（不带数据）
     */
    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setSuccess(true);
        result.setShouldCompensate(false);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        return result;
    }

    /**
     * 成功响应（带数据）
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setSuccess(true);
        result.setShouldCompensate(false);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }




    /**
     * 失败响应（使用预定义错误码）
     */
    public static <T> Result<T> fail(String message) {
        Result<T> result = new Result<>();
        result.setSuccess(false);
        result.setShouldCompensate(true);
        result.setCode(ResultCode.ERROR.getCode());
        result.setMessage(message);
        return result;
    }

    /**
     * 失败响应（使用预定义错误码）
     */
    public static <T> Result<T> fail(ResultCode resultCode) {
        Result<T> result = new Result<>();
        result.setSuccess(false);
        result.setShouldCompensate(true);
        result.setCode(resultCode.getCode());
        result.setMessage(resultCode.getMessage());
        return result;
    }

    /**
     * 失败响应（自定义错误信息）
     */
    public static <T> Result<T> fail(int code, String message) {
        Result<T> result = new Result<>();
        result.setSuccess(false);
        result.setShouldCompensate(true);
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    /**
     * 失败响应（使用预定义错误码和自定义消息）
     */
    public static <T> Result<T> fail(ResultCode resultCode, String message) {
        Result<T> result = new Result<>();
        result.setSuccess(false);
        result.setShouldCompensate(true);
        result.setCode(resultCode.getCode());
        result.setMessage(message);
        return result;
    }


    public static <T> Result<T> success(String msg, T data) {
        Result<T> result = new Result<>();
        result.setSuccess(true);
        result.setShouldCompensate(false);
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(msg);
        return result;
    }
}
